#!/usr/bin/env bash
# Copyright (c) 2021 The Toltec Contributors
# SPDX-License-Identifier: MIT

archs=(rm2)
pkgnames=(linux-mainline)
pkgdesc="reMarkable 2 kernel based on the mainline kernel"
url=https://www.kernel.org
pkgver=6.3.0-2
timestamp=2023-08-23T21:50:09Z
section=kernel
maintainer="Alistair Francis <alistair@alistair23.me>"
makedepends=(build:flex build:bison build:libssl-dev build:bc build:lzop build:libgmp-dev build:libmpc-dev)
installdepends=(kernelctl)
license=GPL-2.0-only
flags=(nostrip)

image=base:v3.2
source=(
    https://github.com/alistair23/linux/archive/1698a3bdd8d66e58873c5bb3ce370b7d48b004cf.tar.gz
    remarkable_defconfig
)
sha256sums=(
    647f6a60adad8c4e37eeaf9114c037888cb33923e364bbf5dc0830b2661c21b7
    f4d8cbb2c0e7bff5b151729b45064eb9e95005cb91899f0a9df038343d95ebf2
)

build() {
    cp remarkable_defconfig arch/arm/configs/
    ARCH=arm make remarkable_defconfig
    ARCH=arm make -j8
}

package() {
    # Prepare files for the kernel archive
    local staging="$srcdir"/staging
    mkdir -p "$staging/boot"

    cp --no-dereference {"$srcdir"/arch/arm,"$staging"}/boot/zImage
    cp --no-dereference "$srcdir"/arch/arm/boot/dts/imx7d-remarkable2.dtb \
        "$staging"/boot/zero-sugar.dtb

    ARCH=arm make -C "$srcdir" modules_install INSTALL_MOD_PATH="$staging"
    rm "$staging"/lib/modules/*/{source,build}

    # Create the kernel archive
    local archive="mainline-${pkgver%-*}.tar.bz2"
    install -d "$pkgdir"/opt/usr/share/kernelctl
    (cd "$staging" && tar --owner root:0 --group root:0 --mtime=$timestamp \
        -cjf "$pkgdir"/opt/usr/share/kernelctl/"$archive" boot/* lib/modules/*)
}

configure() {
    echo "The new kernel files have been copied, but not installed."
    echo "Please use kernelctl to select the kernel to boot."
    echo
    echo "Known issues with the mainline kernel:"
    echo " - No support for low power mode (suspend uses more power then it should)"
    echo " - Shutdown doesn't work (https://github.com/alistair23/linux/issues/4)"
    echo " - Device doesn't always resume correctly from suspend, a reboot is required"
    echo " - Wacom stylus doesn't work in Xochitl (https://github.com/reMarkable/linux/issues/15)"
    echo " - No OTG control support"
    echo " - Folio does not work"
}
